home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1995 / MacHack 1995.toast / Presentations / Presentations ’91 / DAL Files / DALtool 6⁄6 (System 7.x) / DalDemoWindows.c < prev    next >
C/C++ Source or Header  |  1991-06-02  |  11KB  |  508 lines

  1. #include "DalDemo.h"
  2.  
  3. extern int        gNewWindowLeft, gNewWindowTop;
  4. extern int        gLinesInFolder;
  5. extern char     dirty;
  6. extern Rect    gInRect, gInScrollBarRect, gOutRect, gOutScrollBarRect;
  7. extern CharsHandle gInText;
  8.  
  9. /*** WindowType ***/
  10. int WindowType(window)
  11. WindowPtr window;
  12. {
  13.     if (window == NIL) return(NIL_WINDOW);
  14.         
  15.     if (((WindowPeek)window)->windowKind < 0) return(DA_WINDOW);
  16.     
  17.     if (((demoPeek)window)->wType == DEMO_WINDOW) return(DEMO_WINDOW);
  18.     
  19.     return(UNKNOWN_WINDOW);
  20. }
  21.  
  22. /*** CreateWindow ***/
  23. demoPeek CreateWindow()
  24. {
  25.     Rect destRect, viewRect;
  26.     Ptr        wStore;
  27.     WindowPtr    w;
  28.     demoPeek    demoWind;
  29.     
  30.     wStore = NewPtr(sizeof(demoRecord));
  31.     
  32.     if ((w = GetNewWindow(RSRC_BASE, wStore, (WindowPtr) MOVE_TO_FRONT)) == NIL)
  33.         ErrorHandler(NO_WIND);
  34.     if (((screenBits.bounds.right - gNewWindowLeft) < DRAG_THRESH) ||
  35.         ((screenBits.bounds.bottom - gNewWindowTop) < DRAG_THRESH))
  36.         {
  37.             gNewWindowLeft = WINDOW_HOME_LEFT;
  38.             gNewWindowTop = WINDOW_HOME_TOP;
  39.         }
  40.     MoveWindow(w, gNewWindowLeft,gNewWindowTop,LEAVE_WHERE_IT_IS);
  41.     gNewWindowLeft += NEW_WINDOW_OFFSET;
  42.     gNewWindowTop += NEW_WINDOW_OFFSET;
  43.     
  44.     demoWind = (demoPeek)w;
  45.     demoWind->wType = DEMO_WINDOW;
  46.     
  47.     demoWind->vScroll = NewControl( w, &gInScrollBarRect, NIL_STR, 1, 0, 0, 0,
  48.         scrollBarProc, 0L);
  49.     demoWind->vOutScroll = NewControl( w, &gOutScrollBarRect, NIL_STR, 1, 0, 0, 0,
  50.         scrollBarProc, 0L);
  51.  
  52.     ShowWindow(w);
  53.     SetPort(w);
  54.     TextFont(4);
  55.     TextSize(9);
  56.     
  57.     viewRect = gInRect;
  58.     InsetRect(&viewRect, 2, 2);
  59.     demoWind->inputTE = TENew( &viewRect, &viewRect );
  60.     SetClikLoop(NewClikLoop, demoWind->inputTE);
  61.     TEAutoView(TRUE, demoWind->inputTE);
  62.     
  63.     viewRect = gOutRect;
  64.     InsetRect(&viewRect, 2, 2);
  65.     demoWind->outputTE = TENew( &viewRect, &viewRect );
  66.     SetClikLoop(NewClikLoop, demoWind->outputTE);
  67.     TEAutoView(TRUE, demoWind->outputTE);
  68.     
  69.     demoWind->curTE = demoWind->inputTE;
  70.     
  71.     return demoWind;
  72. }
  73.  
  74. /***NewClikLook***/
  75. pascal Boolean NewClikLoop()
  76. {
  77.     WindowPtr    window;
  78.     demoPeek    demoWind;
  79.     TEHandle    te;
  80.     Rect        tempRect;
  81.     Point        mouse;
  82.     GrafPtr        oldPort;
  83.     int            amount;
  84.     RgnHandle    oldClip;
  85.     
  86.     window = FrontWindow();
  87.     if (WindowType(window) != DEMO_WINDOW) return(FALSE);
  88.     
  89.     demoWind = (demoPeek)window;
  90.     te = demoWind->curTE;
  91.     
  92.     GetPort(&oldPort);
  93.     SetPort(window);
  94.     oldClip = NewRgn();
  95.     GetClip(oldClip);
  96.     
  97.     SetRect(&tempRect, -32767, -32767, 32767, 32767);
  98.     ClipRect(&tempRect);
  99.     
  100.     GetMouse(&mouse);
  101.     
  102.     if (te == demoWind->inputTE)
  103.     {
  104.         if (mouse.v < gInRect.top)
  105.         {
  106.             amount = 1;
  107.             CommonAction(demoWind->vScroll, &amount);
  108.             if (amount != 0)
  109.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  110.         }
  111.         else if (mouse.v > gInRect.bottom)
  112.         {
  113.             amount = -1;
  114.             CommonAction(demoWind->vScroll, &amount);
  115.             if (amount != 0)
  116.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  117.         }
  118.     }
  119.     else
  120.     {
  121.         if (mouse.v < gOutRect.top)
  122.         {
  123.             amount = 1;
  124.             CommonAction(demoWind->vOutScroll, &amount);
  125.             if (amount != 0)
  126.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  127.         }
  128.         else if (mouse.v > gOutRect.bottom)
  129.         {
  130.             amount = -1;
  131.             CommonAction(demoWind->vOutScroll, &amount);
  132.             if (amount != 0)
  133.                 TEScroll(0, amount * ((*te)->lineHeight), te);
  134.         }
  135.     }
  136.     
  137.     SetClip(oldClip);
  138.     DisposeRgn(oldClip);
  139.     SetPort(oldPort);
  140.     return(TRUE);
  141. }
  142.     
  143. /*** AdjustScrollBar ***/
  144. AdjustScrollBar(demoWind)
  145. demoPeek demoWind;
  146. {
  147.     short    value, lines, max;
  148.     short     oldValue, oldMax;
  149.     TEPtr    te;
  150.  
  151.     if (demoWind->curTE == demoWind->inputTE)
  152.     {    
  153.         oldValue = GetCtlValue(demoWind->vScroll);
  154.         oldMax = GetCtlMax(demoWind->vScroll);
  155.     }
  156.     else
  157.     {
  158.         oldValue = GetCtlValue(demoWind->vOutScroll);
  159.         oldMax = GetCtlMax(demoWind->vOutScroll);
  160.     }
  161.     te = *(demoWind->curTE);
  162.  
  163.     lines = te->nLines;
  164.  
  165.     if (*(*te->hText + te->teLength - 1) == TE_CARRIAGE_RETURN)
  166.         ++lines;
  167.  
  168.     max = lines - ((te->viewRect.bottom - te->viewRect.top) / te->lineHeight);
  169.     if (max < 0) max = 0;
  170.  
  171.     if (demoWind->curTE == demoWind->inputTE)    
  172.         SetCtlMax(demoWind->vScroll,max);
  173.     else
  174.         SetCtlMax(demoWind->vOutScroll,max);
  175.  
  176.     te = *(demoWind->curTE);
  177.     value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
  178.     if (value < 0) value = 0;
  179.     else
  180.         if (value > max) value = max;
  181.  
  182.     if (demoWind->curTE == demoWind->inputTE)
  183.     {
  184.         SetCtlValue(demoWind->vScroll, value);
  185.         TEScroll(0, (te->viewRect.top - te->destRect.top) - 
  186.             (GetCtlValue(demoWind->vScroll) * te->lineHeight), demoWind->curTE);
  187.     }
  188.     else
  189.     {
  190.         SetCtlValue(demoWind->vOutScroll, value);
  191.         TEScroll(0, (te->viewRect.top - te->destRect.top) - 
  192.             (GetCtlValue(demoWind->vOutScroll) * te->lineHeight), demoWind->curTE);
  193.     }
  194. }
  195.  
  196. /*** CommonAction***/
  197. void CommonAction(control,amount)
  198. ControlHandle control;
  199. short *amount;
  200. {
  201.     short value, max;
  202.     
  203.     value = GetCtlValue(control);
  204.     max = GetCtlMax(control);
  205.     *amount = value - *amount;
  206.     if (*amount < 0)
  207.         *amount = 0;
  208.     else if (*amount > max)
  209.         *amount = max;
  210.     SetCtlValue(control, *amount);
  211.     *amount = value - *amount;
  212. }
  213.  
  214. /*** ScrollProc ***/
  215. pascal void ScrollProc(theControl, theCode)
  216. ControlHandle    theControl;
  217. int                theCode;
  218. {
  219.     int    pageSize;
  220.     int    scrollAmt;
  221.     demoPeek    demoWind;
  222.     TEPtr    te;
  223.     
  224.     if (theCode == 0)
  225.         return ;
  226.     
  227.     demoWind = (demoPeek) (*theControl)->contrlOwner;
  228.     te = *(demoWind->curTE);
  229.     pageSize = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
  230.             
  231.     switch (theCode) {
  232.         case inUpButton: 
  233.             scrollAmt = 1;
  234.             break;
  235.         case inDownButton: 
  236.             scrollAmt = -1;
  237.             break;
  238.         case inPageUp: 
  239.             scrollAmt = pageSize;
  240.             break;
  241.         case inPageDown: 
  242.             scrollAmt = -pageSize;
  243.             break;
  244.         }
  245.     CommonAction(theControl, &scrollAmt);
  246.     if (scrollAmt != 0) 
  247.         TEScroll(0, scrollAmt * te->lineHeight, demoWind->curTE);
  248.  
  249. }
  250.  
  251. /***DoContent***/
  252. DoContent(theWindow, theEvent)
  253. WindowPtr    theWindow;
  254. EventRecord    *theEvent;
  255. {
  256.     int                thePart;
  257.     ControlHandle     theControl;
  258.     demoPeek         demoWind;
  259.     TEPtr            te;
  260.     int                value;
  261.     Point            locmouse;
  262.     Boolean            shiftDown;
  263.     
  264.     if (WindowType(theWindow) != DEMO_WINDOW) return;
  265.     
  266.     demoWind = (demoPeek) theWindow;
  267.     locmouse = theEvent->where;
  268.     GlobalToLocal( &locmouse );
  269.     
  270.     if ((thePart = FindControl(locmouse, theWindow, &theControl)) != 0 && 
  271.         theControl != NIL) 
  272.     {
  273.         switch (thePart)
  274.         {
  275.             case inUpButton:
  276.             case inDownButton:
  277.             case inPageUp:
  278.             case inPageDown:
  279.                 value = TrackControl(theControl, locmouse, (ProcPtr) ScrollProc);
  280.                 break;
  281.             case inThumb:
  282.                 value = GetCtlValue(theControl);
  283.                 thePart = TrackControl(theControl, locmouse, NIL);
  284.                 if (thePart != 0)
  285.                 {
  286.                     value -= GetCtlValue(theControl);
  287.                     te = *(demoWind->curTE);
  288.                     if (value != 0)
  289.                         TEScroll( 0, value * te->lineHeight,demoWind->curTE);
  290.                 }
  291.                 break;
  292.         }
  293.     }
  294.     else if (PtInRect(locmouse, &gInRect))
  295.     {
  296.         if (demoWind->curTE == demoWind->inputTE)
  297.         {
  298.             shiftDown = (theEvent->modifiers & shiftKey) != 0;
  299.             TEClick(locmouse,shiftDown,demoWind->inputTE);
  300.         }
  301.         else
  302.         {
  303.             SwitchToNewArea(demoWind);
  304.             TEClick(locmouse,FALSE,demoWind->inputTE);
  305.         }
  306.     }
  307.     else if (PtInRect(locmouse, &gOutRect))
  308.     {
  309.         if (demoWind->curTE == demoWind->outputTE)
  310.         {
  311.             shiftDown = (theEvent->modifiers & shiftKey) != 0;
  312.             TEClick(locmouse,shiftDown,demoWind->outputTE);
  313.         }
  314.         else
  315.         {
  316.             SwitchToNewArea(demoWind);
  317.             TEClick(locmouse,FALSE,demoWind->outputTE);
  318.         }
  319.     }
  320. }
  321.  
  322. /***SwitchToNewArea***/
  323. void SwitchToNewArea(demoWind)
  324. demoPeek demoWind;
  325. {
  326.     if (demoWind->curTE == demoWind->inputTE)
  327.     {
  328.         TurnOffTextArea(demoWind,TE_INPUT_AREA);
  329.         TurnOnTextArea(demoWind,TE_OUTPUT_AREA);
  330. /*        HandleUpdate((WindowPtr)demoWind);*/
  331.     }
  332.     else
  333.     {
  334.         TurnOffTextArea(demoWind,TE_OUTPUT_AREA);
  335.         TurnOnTextArea(demoWind,TE_INPUT_AREA);
  336. /*        HandleUpdate((WindowPtr)demoWind);*/
  337.     }
  338. }
  339.  
  340. /***TurnOnTextArea***/
  341. void TurnOnTextArea(demoWind,whichArea)
  342. demoPeek demoWind;
  343. int whichArea;
  344. {
  345.     TEPtr    te;
  346.     
  347.     if (whichArea == TE_OUTPUT_AREA)
  348.     {
  349.         te = *demoWind->outputTE;
  350.         te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / 
  351.             te->lineHeight) * te->lineHeight) + te->viewRect.top;
  352.         te->destRect.bottom = te->viewRect.bottom;
  353.         HiliteControl(demoWind->vOutScroll,0);
  354.         AdjustScrollBar(demoWind);
  355.         demoWind->curTE = demoWind->outputTE;
  356.     }
  357.     else
  358.     {
  359.         te = *demoWind->inputTE;
  360.         te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / 
  361.             te->lineHeight) * te->lineHeight) + te->viewRect.top;
  362.         te->destRect.bottom = te->viewRect.bottom;
  363.         HiliteControl(demoWind->vScroll,0);
  364.         AdjustScrollBar(demoWind);
  365.         demoWind->curTE = demoWind->inputTE;
  366.     }
  367.     TEActivate(demoWind->curTE);
  368. }
  369.  
  370. /***TurnOffTextArea***/
  371. void TurnOffTextArea(demoWind, whichArea)
  372. demoPeek demoWind;
  373. int    whichArea;
  374. {
  375.     if (whichArea == TE_OUTPUT_AREA)
  376.     {
  377.         HiliteControl(demoWind->vOutScroll,255);
  378.         TEDeactivate(demoWind->outputTE);
  379.     }
  380.     else
  381.     {
  382.         HiliteControl(demoWind->vScroll,255);
  383.         TEDeactivate(demoWind->inputTE);
  384.     }
  385. }
  386.     
  387.     
  388.  
  389. /***DrawDemoWind***/
  390. void DrawDemoWind(window)
  391. WindowPtr window;
  392. {
  393.     FrameRect(&gInRect);
  394.     FrameRect(&gOutRect);
  395.     DrawControls(window);
  396.     
  397.     TextFont(geneva);
  398.     TextFace(bold);
  399.     MoveTo(gInRect.left - 40, gInRect.top + 12);
  400.     DrawString("\pInput");
  401.     MoveTo(gOutRect.left - 40, gOutRect.top + 12);
  402.     DrawString("\pOutput");
  403.     
  404.     TextFont(monaco);
  405.     TextFace(0);
  406.     TEUpdate(&window->portRect, ((demoPeek)window)->inputTE);
  407.     TEUpdate(&window->portRect, ((demoPeek)window)->outputTE);
  408. }
  409.  
  410. /***HandleCloseWindow***/
  411. HandleCloseWindow(demoWind)
  412. demoPeek    demoWind;
  413. {
  414.     DALCloseLink(demoWind);
  415.     HideWindow( (WindowPtr)demoWind );
  416.     DisposeControl(demoWind->vScroll);
  417.     DisposeControl(demoWind->vOutScroll);
  418.     TEDispose(demoWind->curTE);
  419.     CloseWindow( (WindowPtr)demoWind);
  420.     DisposPtr(demoWind);
  421. }
  422.  
  423. /*** HandleTEKey ***/
  424. HandleTEKey(c)
  425. char c;
  426. {
  427.     WindowPtr     w;
  428.     demoPeek    demoWind;
  429.     int            wType;
  430.     
  431.     w = FrontWindow();
  432.     wType = WindowType(w);
  433.     
  434.     if (wType == DEMO_WINDOW)
  435.     {
  436.         demoWind = (demoPeek) w;
  437.     
  438.         switch (c)
  439.         {
  440.             case TE_TAB_CHAR:
  441.                 SwitchToNewArea(demoWind);
  442.                 break;
  443.             case TE_ENTER_KEY:
  444.                 if (demoWind->curTE == demoWind->outputTE) break;
  445.                 TESetSelect(0,32767,demoWind->curTE);
  446.                 TECopy(demoWind->curTE);
  447.                 gInText = TEGetText(demoWind->curTE);
  448.                 DALSendExecute(demoWind);
  449.                 TEDelete(demoWind->curTE);
  450.                 AdjustScrollBar(demoWind);
  451.                 break;
  452.             default:
  453.                 TEKey(c, demoWind->curTE) ;
  454.                 AdjustScrollBar(demoWind);
  455.                 dirty = 1;
  456.                 break;
  457.         }
  458.     }
  459. }
  460.         
  461.  
  462. /*** HandleActivate ***/        
  463. HandleActivate(w,f)
  464. WindowPtr w;
  465. Boolean f;
  466. {
  467.     demoPeek     demoWind;
  468.     int            wType;
  469.     Rect         theRect;
  470.     
  471.     wType = WindowType(w);
  472.     if (wType == DEMO_WINDOW )
  473.     {
  474.         demoWind = (demoPeek)w;
  475.         if (f)
  476.         {
  477.             SetPort(demoWind);
  478.             if (demoWind->curTE == demoWind->inputTE)
  479.             {
  480.                 TurnOnTextArea(demoWind,TE_INPUT_AREA);
  481.                 ShowControl(demoWind->vScroll);
  482.             }
  483.             else
  484.             {
  485.                 TurnOnTextArea(demoWind,TE_OUTPUT_AREA);
  486.                 ShowControl(demoWind->vOutScroll);
  487.             }
  488.             TEFromScrap();
  489.         }
  490.         else
  491.         {
  492.             if (demoWind->curTE == demoWind->inputTE)
  493.             {
  494.                 TurnOffTextArea(demoWind,TE_INPUT_AREA);
  495.                 HideControl(demoWind->vScroll);
  496.             }
  497.             else
  498.             {
  499.                 TurnOffTextArea(demoWind,TE_OUTPUT_AREA);
  500.                 HideControl(demoWind->vOutScroll);
  501.             }
  502.             ZeroScrap();
  503.             TEToScrap();
  504.         }
  505.     }
  506. }
  507.  
  508.